/*
 * @Author: 369886629@qq.com 369886629@qq.com
 * @Date: 2025-03-05 13:43:55
 * @LastEditors: 369886629@qq.com 369886629@qq.com
 * @LastEditTime: 2025-03-20 14:49:31
 * @FilePath: \sqeybusiness-h5\src\utils\request.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import axios from 'axios'
import { Toast } from 'vant'
// 根据环境不同引入不同api地址
import { baseApi } from '@/config'
import { getToken } from '@/utils/auth'
// create an axios instance
// axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'

const service = axios.create({
  baseURL: baseApi, // url = base api url + request url
  withCredentials: true, // send cookies when cross-domain requests
  timeout: 60000 // request timeout
})

// request拦截器 request interceptor
service.interceptors.request.use(
  config => {
    // 不传递默认开启loading
    if (!config.hideloading) {
      // loading
      Toast.loading({
        message: '加载中...',
        duration: 0,
        loadingType: 'spinner',
        forbidClick: true
      })
    }
    const isToken = (config.headers || {}).isToken === false
    if (getToken() && !isToken) {
      // config.headers['x-token'] = 'sqeybusiness ' +  getToken()
    }

    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)
// respone拦截器
service.interceptors.response.use(
  response => {
    Toast.clear()
    const res = response.data
    if (response.status && res.code !== 200) {
      // 登录超时,重新登录
      return Promise.resolve(res)
    } else {
      return Promise.resolve(res)
    }
  },
  error => {
    Toast.clear()
    console.log('err' + error) // for debug
    return Promise.reject(error)
  }
)

export default service
